<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="collapseFlag?'64px':'200px'">
        <div class="toggle"  @click="collapseFlag = !collapseFlag">|||</div>
        <el-menu
          :default-active="activeIndex"
          unique-opened
          router
          :collapse-transition="false"
          :collapse="collapseFlag"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
        >
          <!-- 一级菜单
          index的作用 index不同 点击每个一级菜单才能展开想赢的二级菜单
        为什么给:index拼接一个字符串 因为elementUI默认index是个字符串 动态绑定item.id这个数值之后会失效 -->
          <el-submenu :index="item.id + ''" v-for="item in menuslist" :key="item.id">
            <!-- 一级菜单标题 -->
            <template slot="title">
              <i :class="iconslist[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="setActive('/'+item2.path)" :index="'/'+item2.path + ''" v-for="item2 in item.children" :key="item2.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuslist: [],
      iconslist: {
        "125": "iconfont icon-users",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      collapseFlag: true,
      activeIndex: ''
   };
  },

  methods: {
    logout() {
      this.$router.push("/login");
      sessionStorage.removeItem("token");
      this.$message.success("退出成功");
    },
    async getMenus() {
      var res = await this.axios.get("menus");
      // console.log(res);
      this.menuslist = res.data; 
      console.log(this.menuslist);
    },
    setActive(index){
      this.activeIndex = index;
      console.log(this.activeIndex);
      //将对应的index存储到本地存储中 页面刷新时重新从本地存储中获取index 显示高亮
      sessionStorage.setItem('activeIndex',index)
      
}
  },

  created() {
    this.getMenus();
    //页面加载时 查找本地存储中当前应该显示高亮的那一栏
    var index = sessionStorage.getItem('activeIndex')
    this.activeIndex = index  }
};
</script>

<style lang='less' scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 10px 20px;
  div {
    display: flex;
    align-items: center;
    color: #ffffff;
    font-size: 20px;
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #ccc;
}
.el-menu {
  border-right: 1px solid #333744;
}
//给字体图标的右侧和文字之间添加间隙
.iconfont{
  margin-right: 10px;
}
.toggle {
  background-color: #333;
  text-align: center;
  color: #fff

}
</style>
